//////////////////////////////////////////////////////////////////////////////////////////////////////////
//		            BulletRemoveConstraint
//////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma warning (disable: 4819)
#include "CKAll.h"
#include "BulletManager.h"
//#include "VirtoolsMotionState.h"

CKObjectDeclaration	*FillBehaviorBulletRemoveConstraintDecl();
CKERROR CreateBulletRemoveConstraintProto(CKBehaviorPrototype **);
int BulletRemoveConstraint(const CKBehaviorContext& BehContext);
//[!output BB_CALLBACKDECL]

CKObjectDeclaration	*FillBehaviorBulletRemoveConstraintDecl()
{
	CKObjectDeclaration *od = CreateCKObjectDeclaration("BulletRemoveConstraint");	
	
	od->SetType(CKDLL_BEHAVIORPROTOTYPE);
	od->SetVersion(0x00000001);
	od->SetCreationFunction(CreateBulletRemoveConstraintProto);
	od->SetDescription("Bullet Remove Constraint");
	od->SetCategory("Bullet/Constraint");
	od->SetGuid(CKGUID(0x363c2777,0x70c85ea1));
	od->SetAuthorGuid(CKGUID(0x457d2f46,0x3ee809a8));
	od->SetAuthorName("gjfeilipu");
	od->SetCompatibleClassId(CKCID_BEOBJECT);

	return od;
}

CKERROR CreateBulletRemoveConstraintProto(CKBehaviorPrototype** pproto)
{
	CKBehaviorPrototype *proto = CreateCKBehaviorPrototype("BulletRemoveConstraint");
	if (!proto) {
		return CKERR_OUTOFMEMORY;
	}

//[!output INPUT_DECL]
//[!output OUTPUT_DECL]
//[!output INPUTP_DECL]
//[!output OUTPUTP_DECL]
	proto->DeclareInput("In");
	proto->DeclareOutput("Out");

	proto->DeclareInParameter("Constraint ID",CKPGUID_INT);

//----	Local Parameters Declaration

//----	Settings Declaration
//[!output BB_FLAGS]
//[!output BB_CALLBACKREG]
	proto->SetFlags(CK_BEHAVIORPROTOTYPE_NORMAL);
	proto->SetFunction(BulletRemoveConstraint);

	*pproto = proto;
	return CK_OK;
}

int BulletRemoveConstraint(const CKBehaviorContext& BehContext)
{
	CKBehavior* beh = BehContext.Behavior;
	CKContext* cex = beh->GetCKContext();
	beh->ActivateInput(0,FALSE);
	beh->ActivateOutput(0,TRUE);
	
	BulletManager* bManager = (BulletManager*)cex->GetManagerByGuid(BulletManagerGUID);//->addRigidBody(btRigidBody *body);
	if(bManager)
	{
			int ID = -1;
			beh->GetInputParameterValue(0, &ID);
			if(ID > 0)
			{
				bManager->removeConstraint(ID);
			}
	}
	return CKBR_OK;
}